package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Doctor;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.Logg;
import com.ruitu.arad.api.RxUtil;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 支付成功
public class PaySuccessActivity extends Base2Activity {
    private TextView tv_ok;

    private String from;// 标识跳转来源

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_pay_success;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        tv_ok = findViewById(R.id.tv_ok);

        setNoNavigationIcon();

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            from = bundle.getString("from");
        }

        setHeadTitle("支付成功");
        reqUserInfo();
        setListeners();
    }

    private void setListeners() {
        setOnClickListener(tv_ok);
    }
    // 获取用户信息
    private Disposable reqUserInfo() {
        return APIRetrofit.getDefault().getUserInfo(Global.HEADER, "1")
                .compose(RxUtil.<BaseResult2<List<Doctor>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Doctor>>>() {
                    @Override
                    public void accept(BaseResult2<List<Doctor>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            List<Doctor> doctorList = result2.getData();
                            if (!isDataEmpty(doctorList)) {
                                Doctor doctor = doctorList.get(0);
                                Global.ORG_BALANCE = doctor.getMoney();
                            }
                        } else {

                        }
                        Logg.i("reqUserInfo.result2.getMsg()" + result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {// 确定
            if (TextUtils.equals("recharge", from)) {// 余额充值页面跳转过来的
            } else if (TextUtils.equals("expert", from)) {// 申请专家解读页面跳转过来的
                startActivity(ExpertExplainActivity.class);
            } else if ("market".equals(from)) {
//                startActivity(MyProductActivity.class);
                // 11-26 改为跳转到 “我的商品”页面
                startActivity(MyProductActivity.class);
            } else {// 预约订单
                startActivity(MyOrderActivity.class);
            }
            finish();
        }
    }
}